﻿using AutoMapper;
using MediatR;
using Traceability.App.Api.Application.Command.Detail.Cultivation;
using Traceability.App.ErrorCount;
using Traceability.App.InfrasIntructure;
using Traceability.App.Model.Cultivation;

namespace Traceability.App.Api.Application.Handler.Detail.Cultivation
{
    public class AddCultivationCommpanyHandler : IRequestHandler<AddCultivationCommpanyCommand, ApiResult<int>>
    {
        IBaseRepository<CultivationCommpanyModel> culComRepo;
        IMapper mapper;

        public AddCultivationCommpanyHandler(IBaseRepository<CultivationCommpanyModel> culComRepo, IMapper mapper)
        {
            this.culComRepo = culComRepo;
            this.mapper = mapper;
        }

        public Task<ApiResult<int>> Handle(AddCultivationCommpanyCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res=new ApiResult<int>();
            var model = mapper.Map<CultivationCommpanyModel>(request);
            int a = culComRepo.Add(model).Result;
            if (a > 0)
            {
                res.Msg = "添加养殖企业信息成功";
                res.Code = ApiEnum.Success;
                res.Datas = a;
                return Task.FromResult(res);
            }
            else
            {
                res.Msg = "添加养殖企业信息失败";
                res.Code = ApiEnum.Fail;
                res.Datas = a;
                return Task.FromResult(res);
            }
        }
    }
}
